$(document).ready(function() {
    function checkPasswordMatch() {
        var password = $("#register-password").val();
        var confirmPassword = $("#register-confirm-password").val();

        if (password !== confirmPassword) {
            $("#password-match-error").show();
            $("#register-confirm-password").addClass("is-invalid");
        } else {
            $("#password-match-error").hide();
            $("#register-confirm-password").removeClass("is-invalid");
        }
    }

    function checkUserId() {
        var userId = $("#register-userid").val();
        var userIdPattern = /^\d{6}$/;

        if (!userIdPattern.test(userId)) {
            $("#userid-error").show();
            $("#register-userid").addClass("is-invalid");
        } else {
            $("#userid-error").hide();
            $("#register-userid").removeClass("is-invalid");
        }
    }

    $("#register-password, #register-confirm-password").on("keyup", checkPasswordMatch);
    $("#register-userid").on("keyup", checkUserId);

    $("#register").on("click", function(event) {
        event.preventDefault(); // 防止默认表单提交
        console.log("注册按钮被点击");
        var userId = $("#register-userid").val();
        var username = $("#register-username").val();
        var password = $("#register-password").val();
        var confirmPassword = $("#register-confirm-password").val();

        // 验证用户ID
        var userIdPattern = /^\d{6}$/;
        if (!userIdPattern.test(userId)) {
            $("#userid-error").show();
            $("#register-userid").addClass("is-invalid");
            return;
        }

        // 验证密码匹配
        if (password !== confirmPassword) {
            $("#password-match-error").show();
            $("#register-confirm-password").addClass("is-invalid");
            return;
        }

        // 提交表单
        $.ajax({
            url: '/user/register',
            type: 'post',
            dataType: 'json',
            cache: false,
            headers: { "cache-control": "no-cache" },
            data: {
                "userId": userId,
                "userName": username,
                "userPwd": password
            },
            beforeSend: function() {
                console.log("发送请求到 /user/register");
            },
            success: function(response) {
                console.log("收到响应: ", response); // 打印收到的响应
                if (response.success) {
                    Swal.fire({
                        title: "注册成功!",
                        text: "请点击继续！",
                        icon: "success",
                        confirmButtonText: "继续！"
                    }).then((result) => {
                        if (result.isConfirmed) {
                            window.location.href = "../web/login.html";
                        }
                    });
                } else {
                    Swal.fire({
                        title: "注册失败!",
                        text: response.message || "请稍后重试。",
                        icon: "error"
                    });
                }
            },
            error: function(xhr, status, error) {
                console.error("AJAX错误: ", error); // 打印错误
                console.error("响应: ", xhr.responseText); // 打印响应文本
                Swal.fire({
                    title: "已存在该用户ID，请改用其他ID",
                    icon: "error"
                });
            }
        });
    });
});
